package com.ruangong.GitClient.app.YanZhaoWang;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.ruangong.GitClient.R;
import com.ruangong.GitClient.YanZhaoWang.YanZhaoWang;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.util.Log;

public class YanZhaoWangMenu extends AppCompatActivity {
    private Spinner spinner1;
    private Spinner spinner2;
    private Spinner spinner3;
    ArrayAdapter<String> adapter2;
    ArrayAdapter<String> adapter3;
    private class LoadSubjectListTask extends AsyncTask<Void, Void, List<String>> {

        private Context context;

        public LoadSubjectListTask(YanZhaoWangMenu context) {
            this.context = context;
        }

        @Override
        protected List<String> doInBackground(Void... voids) {
            Log.d("YanZhaoWangMenu", "run getSub");
            return YanZhaoWang.getSubjectList(context);
        }

        @Override
        protected void onPostExecute(List<String> subjects) {
            super.onPostExecute(subjects);

            if (subjects != null && !subjects.isEmpty()) {
                Log.d("YanZhaoWangMenu", "not null");
                for (String subject : subjects) {
                    Log.d("YanZhaoWangMenu", "Subject: " + subject);
                }
                ArrayAdapter<String> universityAdapter = new ArrayAdapter<>(YanZhaoWangMenu.this, android.R.layout.simple_spinner_item, subjects);
                universityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinner3.setAdapter(universityAdapter);
            } else {
                // 处理获取数据失败的情况
                Toast.makeText(YanZhaoWangMenu.this, "获取学科列表失败", Toast.LENGTH_SHORT).show();
            }
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yan_zhao_wang_menu);

        // 获取Spinner实例
        spinner1 = findViewById(R.id.spinner1);
        spinner2 = findViewById(R.id.spinner2);
        spinner3 = findViewById(R.id.spinner3);

        // 假设这是你的数据
        String[] data1 = {"--选择省市--","北京","天津","河北","山西","内蒙古","辽宁","吉林","黑龙江","上海","江苏","浙江","安徽","福建","江西","山东","河南","湖北","湖南","广东","广西","海南","重庆","四川","贵州","云南","西藏","陕西"};
        String[] data2 = {"--选择招生单位--"};
        String[] data3 = {"--选择学科类别--"};

        // 创建适配器并设置数据
        ArrayAdapter<String> adapter1 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, data1);
        adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, data2);
        adapter3 = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, data3);

        // 设置下拉菜单样式
        adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        // 设置适配器
        spinner1.setAdapter(adapter1);
        spinner2.setAdapter(adapter2);
        //spinner3.setAdapter(adapter3);
        performDisplaySubjects();
        spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                // 获取选择的省份名称
                String selectedProvince = parent.getItemAtPosition(position).toString();
                // 获取选择的省份对应的 ID
                Integer provinceId = YanZhaoWang.findProvinceCodeByName(selectedProvince);
                if(provinceId != -1){
                    performSearchUniversities(provinceId);
                }else spinner2.setAdapter(adapter2);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });



        // 获取按钮实例
        Button buttonCopyLink = findViewById(R.id.buttonCopyLink);

        // 添加按钮点击事件监听器
        buttonCopyLink.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取选中的下拉菜单项
                String selectedProvince = spinner1.getSelectedItem().toString();

                String selectedUniversity = spinner2.getSelectedItem().toString();
                String selectedSubject = spinner3.getSelectedItem().toString();

               // 判断是否选择了占位符
                if (selectedProvince.equals("--选择省市--") || selectedUniversity.equals("--选择招生单位--") || selectedSubject.equals("--选择学科类别--")) {
                    Toast.makeText(YanZhaoWangMenu.this, "请先选择完整的信息", Toast.LENGTH_SHORT).show();
                } else {
                    Integer selectedProvinceID = YanZhaoWang.findProvinceCodeByName(selectedProvince);
                    String selectedSubjectID = YanZhaoWang.getSubjectCode(selectedSubject);
                    // 如果选择完整，则传递信息并跳转到下一个活动
                    Intent intent = new Intent(YanZhaoWangMenu.this, MajorListDisplay.class);
                    intent.putExtra("selectedProvince", selectedProvinceID);
                    intent.putExtra("selectedUniversity", selectedUniversity);
                    intent.putExtra("selectedSubject", selectedSubjectID);
                    startActivity(intent);
                }
            }
        });
    }

    private void performSearchUniversities(int provinceId) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    // 获取指定省份的大学列表
                    List<String> universities = new ArrayList<>();
                    universities.add("--选择招生单位--");
                    universities.addAll(YanZhaoWang.getUniversitiesByProvinceCode(provinceId));

                    // 切换回主线程更新UI
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // 将大学列表设置为第二个下拉菜单的数据源
                            ArrayAdapter<String> universityAdapter = new ArrayAdapter<>(YanZhaoWangMenu.this, android.R.layout.simple_spinner_item, universities);
                            universityAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            spinner2.setAdapter(universityAdapter);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                    // 处理异常，例如显示错误消息
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            spinner2.setAdapter(adapter2);
                        }
                    });
                }
            }
        }).start();
    }

    private void performDisplaySubjects() {
        new LoadSubjectListTask(this).execute();
    }

}
